//
//  CheckoutViewController.m
//  KKZ
//
//  Created by Zhang Chuyang on 11-7-28.
//  Copyright 2011年 Tsinghua University. All rights reserved.
//

#import "CheckoutViewController.h"
#import "UIViewController+BarItem.h"

@implementation CheckoutViewController

@synthesize movieName;
@synthesize oldIndexPath;
@synthesize checkoutButton;

- (id)initWithTitle:(NSString *)astring
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if ( self ) {
        self.movieName = astring;
    }
    
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorColor = [UIColor grayColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    [self addTitleToNavigationBar:self.movieName];
    [self createBarItemOn:kNavBarItemSideLeft frame:kNavBarLeftItemFrame title:@"返回" target:self action:@selector(pop)];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 4;
    }
    else if (section == 1) {
        return 2;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3) {
        return 120;
    }
    return 44;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"订单信息";
    }
    else if (section == 1) {
        return @"支付方式";
    }
    else if (section == 3) {
        return @"用户须知";
    }
    else if (section == 2) {
        return @"支付";
    }
    else {
        return @"联系客服";
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"购买票数";
            cell.detailTextLabel.text = @"1张";
        }
        else if (indexPath.row == 1) {
            cell.textLabel.text = @"影院信息";
            cell.detailTextLabel.text = @"中关村美嘉";
        }
        else if (indexPath.row == 2) {
            cell.textLabel.text = @"开场时间";
            cell.detailTextLabel.text = @"18:30";
        }
        else if (indexPath.row == 3) {
            cell.textLabel.text = @"成交价格";
            cell.detailTextLabel.text = @"50元";
        }
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"支付宝";
        }
        else {
            cell.textLabel.text = @"财富通";
        }
    }
    else if (indexPath.section == 2) {
        cell.textLabel.text = @"支付";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if (indexPath.section == 3) {
        cell.textLabel.text = @"用户须知";
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else if (indexPath.section == 4){
        cell.textLabel.text = @"联系客服";
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"phone.png"]];
        cell.accessoryView = imageView;
        [imageView release];
    }
    
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        
        int newrow = indexPath.row;
        int oldrow = self.oldIndexPath.row;
        
        if (newrow != oldrow) {
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            
            UITableViewCell *oldCell = [self.tableView cellForRowAtIndexPath:self.oldIndexPath];
            oldCell.accessoryType = UITableViewCellAccessoryNone;
            
            oldIndexPath = indexPath;
        }
    }
    else if (indexPath.section == 2) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"进入具体支付界面" 
                                                        message:@"" 
                                                       delegate:nil 
                                              cancelButtonTitle:@"好的" 
                                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
    else if (indexPath.section == 4)
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://15210560852"]]];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (IBAction)checkoutButtonPressed
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"进入具体支付界面" 
                                                    message:@"" 
                                                   delegate:nil 
                                          cancelButtonTitle:@"好的" 
                                          otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}

@end
